package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.member.MemberProjectDto;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private static final String NURSINGCACHE_KEY = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        redisTemplate.delete(NURSINGCACHE_KEY);
        return nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        redisTemplate.delete(NURSINGCACHE_KEY);
        return nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        redisTemplate.delete(NURSINGCACHE_KEY);
        return nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        redisTemplate.delete(NURSINGCACHE_KEY);
        return nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        List<NursingProjectVo> o = (List<NursingProjectVo>)ops.get(NURSINGCACHE_KEY);
        if (o != null&& o.size()>0) {
            return  o;
        }else{
            List<NursingProjectVo> allProjects = nursingProjectMapper.getAllProjects();
            ops.set(NURSINGCACHE_KEY, allProjects);
            return allProjects;
        }

    }

    @Override
    public List<NursingProject> MemberOrderPageList(MemberProjectDto dto) {
        // 1. 设置分页参数：当前页、每页条数
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        // 2. 构建查询参数对象（假设你的 mapper 使用 NursingProject 作为查询条件）
        NursingProject nursingProject = new NursingProject();
        nursingProject.setName(dto.getName());
        nursingProject.setStatus(dto.getStatus());
        // 可以根据需要设置其他字段...

        // 3. 调用 DAO 层方法查询数据（这个方法应该是你自己写的，未分页的全量查询）
        List<NursingProject> list = nursingProjectMapper.selectNursingProjectList(nursingProject);

        // 4. 返回结果即可（PageHelper 会自动拦截并分页）
        return list;
    }
}
